package com.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.common.Result;
import com.demo.entity.Paper;
import com.demo.mapper.PaperMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 论文管理
 */
@RestController
@RequestMapping("/newspaper")
public class NewspaperController {
  /**
   * 服务对象
   */
  @Resource
  PaperMapper paperMapper;
  
  // 新增
  @PostMapping
  public Result<?> insert(@RequestBody Paper paper) {
    try {
      paperMapper.insert(paper);
      return Result.success();
    } catch (Exception e) {
      return Result.error("201","");
    }
  
  }
  
  //  删除 ById
  @DeleteMapping("/{id}")
  public Result<?> delete(@PathVariable Integer id) {
    paperMapper.deleteById(id);
    return Result.success();
  }
  
  // 修改
  @PutMapping
  public Result<?> updata(@RequestBody Paper paper) {
    try {
      paperMapper.updateById(paper);
      return Result.success();
    } catch (Exception e) {
      System.out.println(e);
      return Result.error("201", "");
    }
  
  }
  
  /**
   * 分页,默认1页,10个数据,  默认根据 Name(论文名称) 查询
   *
   * @param pageNum  页数
   * @param pageSize 页面显示数
   * @param search   搜索关键词
   * @param sort     搜索类别
   * @return
   */
  @GetMapping
  public Result<?> findPageByaName(@RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "10") Integer pageSize,
                                   @RequestParam(defaultValue = "") String search,
                                   @RequestParam(defaultValue = "Name") String sort) {
    LambdaQueryWrapper<Paper> wrapper = Wrappers.<Paper>lambdaQuery();
    //根据Id逆序排列
    wrapper.orderByDesc(Paper::getId);
    if (StrUtil.isNotBlank(search)) {
      switch (sort) {
        case "Name": //根据论文名称
          wrapper.like(Paper::getName, search);
          break;
        case "Magazine"://根据刊物名称搜索
          wrapper.like(Paper::getMagazine, search);
          break;
        case "Level"://刊物级别
          wrapper.like(Paper::getLevel, search);
          break;
        case "Author"://作者
          wrapper.like(Paper::getAuthor, search);
          break;
        case "Category"://刊物类别
          wrapper.like(Paper::getCategory, search);
          break;
        case "ISSN"://刊号
          wrapper.like(Paper::getIssn, search);
          break;
      }
    }
    
    Page<Paper> AdminPage = paperMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
    
    return Result.success(AdminPage);
  }
  
  // 根据name 查询是否有这个主键
  @GetMapping("/name/{name}")
  public Result<?> getName(@PathVariable String name) {
    Paper paper = paperMapper.selectOne(Wrappers.<Paper>lambdaQuery().eq(Paper::getName, name));
    if(paper == null){
      //表示不含有此主键,可以录入
      return Result.success(paper);
    }
    //表示表中已存在此主键,不可以录入
    return Result.errorData("201", paper);
  }
  
  //根据id获得paper
  @GetMapping("/id/{id}")
  public Result<?> getById(@PathVariable  String id) {
    Paper paper =  this.paperMapper.selectById(id);
    return Result.success(paper);
  }
  
}

